const fs = require('fs')
const http = require('http')

const server =  http.createServer((request, response) => {
    // const {pathname} = new URL(request.url,'http://127.0.0.1:3000')
    // if(pathname === '/'){
    //     const data = fs.readFileSync('./10-table.html')
    //     response.end(data)
    // }else if(pathname === '/10-table.css'){
    //     const data = fs.readFileSync('./10-table.css')
    //     response.end(data)
    // }else if(pathname === '/10-table.js'){
    //     const data = fs.readFileSync('./10-table.js')
    //     response.end(data)
    // }else{
    //     response.statusCode = 404
    //     response.end('404 Not Found')
    // }
    // 再优化
    const {pathname} = new URL(request.url,'http://127.0.0.1:3000')
    let filePath = __dirname + pathname
    fs.readFile(filePath,(err,data) => { 
        if(err){
            response.statusCode = 404
            response.end('404 Not Found')
        }else{
            response.end(data)
        }
     })
})

server.listen(3000, () => {
    console.log('server is running at http://127.0.0.1:3000')
})